package com.thinksns.sociax.t4.android.gift;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.thinksns.sociax.android.R;
import com.thinksns.sociax.modle.CashConfig;
import com.thinksns.sociax.modle.CashToRatio;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.presenter.GoldWithDrawPresenter;
import com.thinksns.sociax.t4.android.view.GoldWithDrawView;
import com.thinksns.sociax.t4.component.ExchangeItemView;
import com.thinksns.sociax.t4.unit.UiUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by jess on 16/4/25.
 */
public class GoldWithDrawActivity extends AppCompatActivity implements GoldWithDrawView {
    public static final String BUNDLE_SCORE = "myScore";
    public static final String BUNDLE_ACCOUNT_TYPE= "account_type";
    public static final String BUNDLE_ACCOUNT = "account";
    @Nullable
    @BindView(R.id.tv_gold_exchange_count)
    TextView mGoldTV;
    @Nullable
    @BindView(R.id.ll_star_exchange_regular)
    LinearLayout mRegularLL;
    @Nullable
    @BindView(R.id.tv_gold_exchange_description)
    TextView mDescriptionTV;
    private String mExchangeGold;//提现金币
    private String mExchangeAmount;//对应金额
    private ProgressDialog mProgressBuilder;
    private AlertDialog mDialog;
    private GoldWithDrawPresenter mPresenter;
    private String mAccount;
    private int mAccountType;
    private String mMyScore;
    private CashConfig mCashConfig;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_with_draw);
        ButterKnife.bind(this);
        initData();
    }
    protected void initData() {
        initDialog();
        mPresenter = new GoldWithDrawPresenter(this);
        mAccount = getIntent().getStringExtra(BUNDLE_ACCOUNT);//获得上一个页面返回的账号信息
        mAccountType = getIntent().getIntExtra(BUNDLE_ACCOUNT_TYPE, 0);
        mMyScore = getIntent().getStringExtra(BUNDLE_SCORE);
        updatedGold("" + 0);//刷新金币信息
    }


    /**
     * 初始化兑换的规则
     */
    @Override
    public void initRegular(CashConfig cashConfig) {
        if (cashConfig == null)
            return;
        this.mCashConfig = cashConfig;
        mDescriptionTV.setText(getString(R.string.str_gold_amount_useable));
        for (int x = 0; x < mCashConfig.getCash_exchange_ratio_list().size(); x++) {
            final CashToRatio s = mCashConfig.getCash_exchange_ratio_list().get(x);
            ExchangeItemView view = new ExchangeItemView(UiUtils.getContext());
            mRegularLL.addView(view);
            view.setIcon1(R.drawable.ic_me_goldcoins);
            view.setIcon2Visible(false);
            view.setTitle("x" + s.getGold());
            view.setSubtitle("¥ " + s.getCash() + ".00");
            if (x == mCashConfig.getCash_exchange_ratio_list().size() - 1) {
                view.cleanDivider(true);
            }
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkGold(s);
                }
            });
        }
    }


    /**
     * 初始化dialog
     */
    private void initDialog() {
        //进度条
        mProgressBuilder = new ProgressDialog(this);
        mProgressBuilder.setMessage(getString(R.string.str_with_drawing));
        mProgressBuilder.setCanceledOnTouchOutside(false);

        AlertDialog.Builder builder = new AlertDialog.Builder(GoldWithDrawActivity.this);
        builder.setNegativeButton(getString(R.string.cancel), null);
        builder.setPositiveButton((getString(R.string.ok)), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mPresenter.exchange(mExchangeGold, mExchangeAmount);//请求服务器提现
            }
        });
        mDialog = builder.create();
        mDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 检查金币的数量,提示用户
     *
     * @param s
     */
    private void checkGold(CashToRatio s) {
        try {
            if (Integer.valueOf(Thinksns.getMy().getUserCredit().getScore_value()) < Integer.valueOf(s.getGold())) {
                showMessage("您的金币数量不足~");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        showPrompt(s);
    }

    /**
     * 提示用户兑换的金币数
     */
    public void showPrompt(CashToRatio s) {
        mExchangeGold = s.getGold();//想兑换的金币数
        mExchangeAmount = s.getCash();//想兑换的现金数
        mDialog.setTitle(getString(R.string.str_is_top_up));
        mDialog.setMessage(String.format(UiUtils.getString(R.string.str_gold_with_draw_detail), s.getGold(), s.getCash(),
                mAccountType == BindAccountActivity.ACCOUNT_ALIPAY ? "支付宝" + mAccount + "账号" : "微信" + mAccount + "账号"
        ));
        mDialog.show();
    }


    @Override
    public void updatedGold(String gold) {
        try {
            mMyScore = (Integer.valueOf(mMyScore) - Integer.valueOf(gold)) + "";
        } catch (Exception e) {
            e.printStackTrace();
        }
        mGoldTV.setText(mMyScore);
    }


    @Nullable
    @OnClick({R.id.tv_title_left})
    public void onclick(View v) {
        switch (v.getId()) {
            case R.id.tv_title_left:
                killMyself();
                break;
        }
    }

    @Override
    public void showLoading() {
        mProgressBuilder.show();
    }

    @Override
    public void hideLoading() {
        mProgressBuilder.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(Intent intent) {
        startActivity(intent);
    }

    @Override
    public void killMyself() {
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
